﻿Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports BBDD
Imports System.Collections.Generic
Imports Controladores
Imports System.Data.SqlClient

 _

Class sincronizarDatos
    Inherits System.Web.UI.Page
    Public Shared CADENA_CONEXION_BOLSA As String = "Server=UAXINVER;database=Bolsa;Integrated security = yes"
    Public Shared CADENA_CONEXION_UAXINVER As String = "Server=UAXINVER;database=UAX_INVERSIONES;Integrated security = yes"

    Public Shared cnn As SqlConnection
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Session("privUser") Is Nothing Or Integer.Parse(Session("privUser").ToString()) < 3 Then
            Response.Redirect("index.aspx")
        End If
    End Sub 'Page_Load

    Protected Sub boton_sincronizar_Click(ByVal sender As Object, ByVal e As EventArgs)
  
        Dim que As String = "DELETE FROM Acciones WHERE 1=1"
        '
        cnn = New SqlConnection(CADENA_CONEXION_UAXINVER)
        cnn.Open()
        Dim com As SqlCommand = New SqlCommand(que, cnn)
        Dim res As Integer = com.ExecuteNonQuery()
        If cnn.State = ConnectionState.Open Then
            cnn.Close()
        End If
        '

        '
        '       Recuperar datos de la base de datos antigua.
        '
        Dim query2 As String = "SELECT * FROM DBBolsa"
        '
        cnn = New SqlConnection(CADENA_CONEXION_BOLSA)
        cnn.Open()
        Dim listaAcciones As List(Of Accion) = New List(Of Accion)
        Dim com3 As New SqlCommand(query2, cnn)
        Dim sqldr As SqlDataReader = com3.ExecuteReader()
        Dim lista As List(Of String()) = New List(Of String())
        Dim datos(sqldr.VisibleFieldCount) As String
        Dim y As Integer = 0
        While sqldr.Read()
            Dim i As Integer
            For i = 0 To sqldr.VisibleFieldCount - 1
                datos(i) = sqldr(i)
            Next i

            Dim arr(3) As String


            arr(0) = datos(1)
            arr(1) = datos(2)
            arr(2) = datos(0)

            listaAcciones.Add(New Accion(arr))
            lista.Add(datos)
            y = y + 1
        End While
        If cnn.State = ConnectionState.Open Then
            cnn.Close()
        End If

        ' Copiar datos a nuestra base de datos
      
        For i = 0 To listaAcciones.Count - 1
           
            Dim insert As String = "INSERT INTO Acciones(id_accion, precio, nombre) VALUES (@id_accion , @precio, @nombre)"
            '
            cnn = New SqlConnection(CADENA_CONEXION_UAXINVER)
            cnn.Open()
            Dim com2 As SqlCommand = New SqlCommand(insert, cnn)

            com2.Parameters.Add("@id_accion", SqlDbType.NVarChar, 50)
            com2.Parameters(0).Value = listaAcciones(i).getId().ToString

            com2.Parameters.Add("@precio", SqlDbType.Money)
            com2.Parameters(1).Value = listaAcciones(i).getValor()

            com2.Parameters.Add("@nombre", SqlDbType.NVarChar, 50)
            com2.Parameters(2).Value = listaAcciones(i).getnombre().ToString

            Dim res2 As Integer = com2.ExecuteNonQuery()
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If

        Next
        '

        Dim hayActuCompras As Boolean = Consultas.actualizarComprasPendientes()
        Dim hayActuVenta As Boolean = Consultas.actualizarVentasPendientes()

        Session("error") = "Base de datos sincronizada"

        Response.Redirect("admin.aspx")
    End Sub 'boton_sincronizar_Click 
End Class 'sincronizarDatos